////// var layer = require('./layer/layer.js');
//$(function(){
//    if(typeof $.cookie('adobe_20160104') == 'undefined' && (location.host == 'shijue.me')) {
//        var url = '';
//        if(location.host == 'vcg-design.shijue.me') {
//            url = '/page/contestv/views/index?id=' + 'feaedb0d9f5a480081c352c07e8fef41' + '&r=adobe&p=index';
//            // layer.open({
//            //     type: 1,
//            //     skin: 'layui-layer-rim', //加上边框
//            //     area: ['340px', '500px'], //宽高
//            //     content: 'QQQQ'
//            // });
//        }else {
//            url = '/page/contestv/views/index?id=c322554cda084e17a9759f020ce1fc25&r=adobe2017&p=index&_from=uglyload';
//            location.href = url;
//        }
//
//    }
//
//});
$(function(){
    $('.sucai-result .pics').html('');
    String.prototype.format = function(args) {
        var result = this;
        if (arguments.length > 0) {
            if (arguments.length == 1 && typeof (args) == "object") {
                for (var key in args) {
                    if(args[key]!=undefined){
                        var reg = new RegExp("({" + key + "})", "g");
                        result = result.replace(reg, args[key]);
                    }
                }
            }
            else {
                for (var i = 0; i < arguments.length; i++) {
                    if (arguments[i] != undefined) {
                        var reg= new RegExp("({)" + i + "(})", "g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
        }
        return result;
    }
    //$('.pics .span_3_of_12')[0] && $('.pics .span_3_of_12')[0].remove();
    //$('.pics').append('<div class="col span_3_of_12" data-reactid=".2.0.0.$8"><div data-reactid=".2.0.0.$8.0"><div class="item" data-reactid=".2.0.0.$8.0.0"><a href="/community/photo-details/61b65ae491d74b2894ed74092efea9e6_d" target="_blank" data-reactid=".2.0.0.$8.0.0.0"><div class="pic" data-reactid=".2.0.0.$8.0.0.0.0"><img data-original="http://pic.shijue.me/picurl/c29ebfc2f43a4475bb7b3f5d968a2e5f_d---gif?code=ddd1caad9ec1cffd" width="100%" src="http://pic.shijue.me/picurl/c29ebfc2f43a4475bb7b3f5d968a2e5f_d---gif?code=ddd1caad9ec1cffd" class="lazy" data-reactid=".2.0.0.$8.0.0.0.0.0" style="display: inline;"><span data-reactid=".2.0.0.$8.0.0.0.0.1"></span></div></a></div><div class="title with-img p-sm  mobile-hide" data-reactid=".2.0.0.$8.0.1"><a target="_blank" href="/community/user-details/4e9f6c33a465381f82250dc853b3f8985_d" data-reactid=".2.0.0.$8.0.1.0"><div class="img" data-reactid=".2.0.0.$8.0.1.0.0"><img src="http://pic.shijue.me/picurl/avatar/4e9f6c33a465381f82250dc853b3f8985_d_1473143108301---jpg?code=bdec27c85e0c3de3" width="100%" data-reactid=".2.0.0.$8.0.1.0.0.0"></div></a><a href="/community/photo-details/61b65ae491d74b2894ed74092efea9e6_d" target="_blank" class="fontW70-13" data-reactid=".2.0.0.$8.0.1.1"><span class="item-sucai-title" data-reactid=".2.0.0.$8.0.1.1.0">有温度的年味 可触摸的情感</span></a><a href="/community/user-details/4e9f6c33a465381f82250dc853b3f8985_d" target="_blank" class="fontW50-13" data-reactid=".2.0.0.$8.0.1.3"><span data-reactid=".2.0.0.$8.0.1.3.0">视觉ME</span></a><span data-reactid=".2.0.0.$8.0.1.4"></span></div><span data-reactid=".2.0.0.$8.0.2"></span></div></div>');
    var createItem = function (item) {
        var tplStr = ['<div class="col span_3_of_12">',
            '<div class="item">',
            '<a target="_blank" href="{0}">',
            '<div class="pic" >',
            '<img width="100%" src="//cdn.shijue.me/images/svg/empty.svg"}>',
            '<img src="{1}" width="100%" class="nature">',
            '</div>',
            '</a>',
            '</div>',
            '<div class="title with-img p-sm mobile-hide">',
            '<a href="{2}" target="_blank">',
            '<div class="img"><img width="100%" src="{3}"/></div>',
            '</a>',
            '<a style="white-space: nowrap;" href="{4}" target="_blank" class="fontW70-13" onClick="this.style.color=\'rgb(127,127,127)\';">{5}</a>',
            '<br>',
            '<a href="{6}" target="_blank" class="fontW50-13">{7}</a>',
            '</div>',
            '</div>'].join('')

        return tplStr.format(item.href,item.picLink,item.authorLink,item.authorAvatar,item.href,item.title,item.authorLink,item.author)
    }
    var ajaxFun = function (url) {
        var fuEl = $('.sucai-result .pics');
        $.ajax({
            url: url,
            data: {
                orderby : 'recommendTime',
                license : '-1',
                type: 'json',
                page: 1,
                size: 24
            }, success: function (resData) {
                if (!resData || !resData.dataArray || !resData.dataArray.length) {
                    //ajaxFun('/community/user-details/38f2d9fa94cc5ba76240707ea0cac3826_d/likes');
                } else {
                    resData = resData.dataArray;
                    for (var i = 0, obj; obj = resData[i]; i++) {
                        var data = {
                            title: obj.title,
                            picLink : obj.url,
                            href: '/community/photo-details/' + obj.id,
                            author: obj.uploaderInfo.nickName,
                            authorAvatar : obj.uploaderInfo.avatar,
                            authorLink: '/community/user-details/' + obj.uploaderInfo.id
                        };
                        fuEl.append(createItem(data));
                    }

                    $.ajax({
                        url: '//node.shijue.me/advertise/getList',
                        dataType: 'jsonp',
                        data: {
                            m : 'index',
                            page: 1,
                            size: 10
                        }, success: function (nresData) {
                            if(nresData.status == 200){
                                var itemStr = '';
                                for (var j = 0, jobj; jobj = nresData.data[j]; j++) {
                                    itemStr += createItem(jobj);
                                }
                                if(itemStr){
                                    fuEl.prepend(itemStr);
                                }
                            }
                        }
                    })

                    var spanEl = fuEl.find('.span_3_of_12:first');
                    var resizeFn = function () {
                        var height = parseInt(spanEl.css('height'));
                        fuEl.css({'height': 3 * height, 'visibility': 'visible'});
                    }
                    $(window).resize(resizeFn);

                    var iobj = new Image();
                    iobj.onload = resizeFn;
                    iobj.src = '//cdn.shijue.me/images/svg/empty.svg';
                }
            }
        })
    }
   //ajaxFun('/community/user-details/38f2d9fa94cc5ba76240707ea0cac3826_d/likes');
    ajaxFun('/community/search');
    // ajaxFun('/community/discovery');
})